{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "from collections import deque\n",
    "import os\n",
    "import pathlib\n",
    "import shutil\n",
    "\n",
    "from IPython import display as ipydisplay\n",
    "import torch\n",
    "\n",
    "from utils_env import MyEnv\n",
    "from utils_drl import Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = 4\n",
    "model_name = f\"model_{target:03d}\"\n",
    "model_path = f\"./models/{model_name}\"\n",
    "# model_path = f\"./models/model_004\"\n",
    "device = torch.device(\"cpu\")\n",
    "env = MyEnv(device)\n",
    "agent = Agent(env.get_action_dim(), device, 0.99, 0, 0, 0, 1, model_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Avg. Reward: 220.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "'rm' 不是内部或外部命令，也不是可运行的程序\n",
      "或批处理文件。\n"
     ]
    }
   ],
   "source": [
    "obs_queue = deque(maxlen=5)\n",
    "avg_reward, frames = env.evaluate(obs_queue, agent, render=True)\n",
    "print(f\"Avg. Reward: {avg_reward:.1f}\")\n",
    "\n",
    "!rm -r eval_*\n",
    "target_dir = f\"eval_{target:03d}\"\n",
    "os.mkdir(target_dir)\n",
    "for ind, frame in enumerate(frames):\n",
    "    frame.save(os.path.join(target_dir, f\"{ind:06d}.png\"), format=\"png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_to_mp4 = os.path.join(target_dir, \"movie.mp4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "系统找不到指定的路径。\n",
      "'rm' 不是内部或外部命令，也不是可运行的程序\n",
      "或批处理文件。\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'tmp_eval_movie.mp4'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mFileNotFoundError\u001B[0m                         Traceback (most recent call last)",
      "\u001B[1;32mD:\\Anaconda3\\envs\\dqn-breakout\\lib\\shutil.py\u001B[0m in \u001B[0;36mmove\u001B[1;34m(src, dst, copy_function)\u001B[0m\n\u001B[0;32m    790\u001B[0m     \u001B[1;32mtry\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 791\u001B[1;33m         \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mrename\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mreal_dst\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    792\u001B[0m     \u001B[1;32mexcept\u001B[0m \u001B[0mOSError\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m: [WinError 2] 系统找不到指定的文件。: 'tmp_eval_movie.mp4' -> 'eval_004\\\\movie.mp4'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m                         Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_5880/75225554.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      5\u001B[0m     \u001B[0mget_ipython\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0msystem\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'rm -r tmp_eval_frames'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      6\u001B[0m     \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mmkdir\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtarget_dir\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 7\u001B[1;33m     \u001B[0mshutil\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mmove\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"tmp_eval_movie.mp4\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mpath_to_mp4\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      8\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Anaconda3\\envs\\dqn-breakout\\lib\\shutil.py\u001B[0m in \u001B[0;36mmove\u001B[1;34m(src, dst, copy_function)\u001B[0m\n\u001B[0;32m    809\u001B[0m             \u001B[0mrmtree\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    810\u001B[0m         \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 811\u001B[1;33m             \u001B[0mcopy_function\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mreal_dst\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    812\u001B[0m             \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0munlink\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    813\u001B[0m     \u001B[1;32mreturn\u001B[0m \u001B[0mreal_dst\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Anaconda3\\envs\\dqn-breakout\\lib\\shutil.py\u001B[0m in \u001B[0;36mcopy2\u001B[1;34m(src, dst, follow_symlinks)\u001B[0m\n\u001B[0;32m    433\u001B[0m     \u001B[1;32mif\u001B[0m \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0misdir\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdst\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    434\u001B[0m         \u001B[0mdst\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mjoin\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdst\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mbasename\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 435\u001B[1;33m     \u001B[0mcopyfile\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mdst\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mfollow_symlinks\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mfollow_symlinks\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    436\u001B[0m     \u001B[0mcopystat\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mdst\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mfollow_symlinks\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mfollow_symlinks\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    437\u001B[0m     \u001B[1;32mreturn\u001B[0m \u001B[0mdst\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mD:\\Anaconda3\\envs\\dqn-breakout\\lib\\shutil.py\u001B[0m in \u001B[0;36mcopyfile\u001B[1;34m(src, dst, follow_symlinks)\u001B[0m\n\u001B[0;32m    262\u001B[0m         \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0msymlink\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mreadlink\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mdst\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    263\u001B[0m     \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 264\u001B[1;33m         \u001B[1;32mwith\u001B[0m \u001B[0mopen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0msrc\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'rb'\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mfsrc\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mopen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdst\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'wb'\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mfdst\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    265\u001B[0m             \u001B[1;31m# macOS\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    266\u001B[0m             \u001B[1;32mif\u001B[0m \u001B[0m_HAS_FCOPYFILE\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: 'tmp_eval_movie.mp4'"
     ]
    }
   ],
   "source": [
    "if not os.path.exists(path_to_mp4):\n",
    "    shutil.move(target_dir, \"tmp_eval_frames\")\n",
    "    # Generate an mp4 video from the frames\n",
    "    !ffmpeg -i \"./tmp_eval_frames/%06d.png\" -pix_fmt yuv420p -y ./tmp_eval_movie.mp4 > /dev/null 2>&1\n",
    "    !rm -r tmp_eval_frames\n",
    "    os.mkdir(target_dir)\n",
    "    shutil.move(\"tmp_eval_movie.mp4\", path_to_mp4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HTML_TEMPLATE = \"\"\"<video alt=\"{alt}\" autoplay loop controls style=\"height: 400px;\">\n",
    "  <source src=\"data:video/mp4;base64,{data}\" type=\"video/mp4\" />\n",
    "</video>\"\"\"\n",
    "\n",
    "def show_video(path_to_mp4: str) -> None:\n",
    "    \"\"\"show_video creates an HTML element to display the given mp4 video in IPython.\"\"\"\n",
    "    mp4 = pathlib.Path(path_to_mp4)\n",
    "    video_b64 = base64.b64encode(mp4.read_bytes())\n",
    "    html = HTML_TEMPLATE.format(alt=mp4, data=video_b64.decode('ascii'))\n",
    "    ipydisplay.display(ipydisplay.HTML(data=html))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "show_video(path_to_mp4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}